package com.cartagena.financo.dao;

import java.util.List;

import net.vidageek.mirror.dsl.Mirror;

import org.hibernate.Session;

import com.cartagena.financo.repository.BaseRepository;

public abstract class BaseDao<T> implements BaseRepository<T> {
    
    protected final Session session;
    private final Class<?> clazz;

    BaseDao(Session session) {
        this.session = session;
        this.clazz = new Mirror().on(getClass()).reflect().parentGenericType().atPosition(0);
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public T get(Long id) {
        return (T) session.get(this.clazz, id);
    }

    @Override
    public void add(T t) {
        this.session.save(t);
    }

    @Override
    public void update(T t) {
        this.session.update(t);
    }

    @Override
    public void remove(T t) {
        this.session.delete(t);
    }
    
    @Override
    @SuppressWarnings("unchecked")
    public List<T> listAll() {
        return this.session.createCriteria(this.clazz).list();
    }

}
